package com.szh.zx.javacorecorret.chap2.two;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

// 章节2.2.7
// 方法 reset 作用是 重置屏障
public class CyclicBarrierTest7 {
	public static void main(String[] args) throws InterruptedException {
		MyService7 service = new MyService7();

		MyThread6 a = new MyThread6(service);
		a.setName("A");

		MyThread7 b = new MyThread7(service);
		b.setName("B");

		a.start();
		b.start();

		Thread.sleep(2000);
		service.cyclicBarrier.reset();
	}
}
class MyService7 {
	public CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Runnable() {
		@Override
		public void run() {
			System.out.println("   彻底结束了 "+ System.currentTimeMillis());

		}
	});
	public void testMethod(){
		try {
			System.out.println(Thread.currentThread().getName() + "准备 " + System.currentTimeMillis());
			cyclicBarrier.await();
			System.out.println(Thread.currentThread().getName() + "结束 " + System.currentTimeMillis());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
	}
}
class MyThread8 extends Thread{
	MyService7 service =new MyService7();

	@Override
	public void run() {
		service.testMethod();
	}

	public MyThread8(MyService7 service) {
		this.service = service;
	}
}
class MyThread7 extends Thread{
	MyService7 service =new MyService7();

	@Override
	public void run() {
		service.testMethod();
	}

	public MyThread7(MyService7 service) {
		this.service = service;
	}
}
class MyThread6 extends Thread{
	MyService7 service =new MyService7();

	@Override
	public void run() {
		service.testMethod();
	}

	public MyThread6(MyService7 service) {
		this.service = service;
	}
}
